﻿Public Class FormularioInicio
    Dim chivato As Boolean = False
#Region "VARIABLES"
    Dim MiTablero As New CTablero
    Dim Boton_Tablero As Button
    Private Movimientos As New List(Of String)
    Dim Tablero As New List(Of Integer)
    Dim Fantasmitos As Integer = 5

#End Region
    'Public Function PosicionPacman()

    '    Return Posicion
    'End Function
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Moviment of PacManColorsX
        Dim Posicion As Integer = 0

        For i = 0 To GBMapa.Controls.Count - 1
            If GBMapa.Controls(i).BackColor = Color.Orange Then
                Posicion = i
            End If
        Next
        Select Case e.KeyCode
            Case Keys.A

                If MiTablero.Izquierda(GBMapa.Controls(Posicion).BackColor = Color.Orange AndAlso GBMapa.Controls(Posicion - 1).BackColor = Color.Transparent Or GBMapa.Controls(Posicion - 1).BackColor = Color.Thistle) Then
                    If GBMapa.Controls(Posicion - 1).BackColor = Color.Thistle Then
                        lblPuntos.Text = lblPuntos.Text + 5
                    End If
                    GBMapa.Controls(Posicion).BackColor = Color.Transparent
                    GBMapa.Controls(Posicion - 1).BackColor = Color.Orange
                End If
            Case Keys.D
                If MiTablero.Derecha(GBMapa.Controls(Posicion).BackColor = Color.Orange AndAlso GBMapa.Controls(Posicion + 1).BackColor = Color.Transparent Or GBMapa.Controls(Posicion + 1).BackColor = Color.Thistle) Then
                    If GBMapa.Controls(Posicion + 1).BackColor = Color.Thistle Then
                        lblPuntos.Text = lblPuntos.Text + 5
                    End If
                    GBMapa.Controls(Posicion).BackColor = Color.Transparent
                    GBMapa.Controls(Posicion + 1).BackColor = Color.Orange

                End If
            Case Keys.S
                If Posicion > 181 Then
                    Exit Sub
                End If
                If MiTablero.Abajo(GBMapa.Controls(Posicion).BackColor = Color.Orange AndAlso GBMapa.Controls(Posicion + 14).BackColor = Color.Transparent Or GBMapa.Controls(Posicion + 14).BackColor = Color.Thistle, Posicion) Then
                    If GBMapa.Controls(Posicion + 14).BackColor = Color.Thistle Then
                        lblPuntos.Text = lblPuntos.Text + 5
                    End If
                    GBMapa.Controls(Posicion).BackColor = Color.Transparent
                    GBMapa.Controls(Posicion + 14).BackColor = Color.Orange

                End If
            Case Keys.W
                If Posicion < 15 Then
                    Exit Sub
                End If
                If MiTablero.Arriba(GBMapa.Controls(Posicion).BackColor = Color.Orange AndAlso GBMapa.Controls(Posicion - 14).BackColor = Color.Transparent Or GBMapa.Controls(Posicion - 14).BackColor = Color.Thistle, Posicion) Then
                    If GBMapa.Controls(Posicion - 14).BackColor = Color.Thistle Then
                        lblPuntos.Text = lblPuntos.Text + 5
                    End If
                    GBMapa.Controls(Posicion).BackColor = Color.Transparent
                    GBMapa.Controls(Posicion - 14).BackColor = Color.Orange

                End If

        End Select



    End Sub

#Region "Creation Map"
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Declaration of variables
        Static PosicionX As Integer = 0
        Static posicionY As Integer = 0
        Static CambioLinea As Integer = 0
        Static tags As Integer = 0
        'Creation of map
        For i = 0 To 195
            Boton_Tablero = New Button
            Boton_Tablero.AutoSize = True
            Boton_Tablero.Text = ""
            Boton_Tablero.Name = "BotonTablero"
            Boton_Tablero.Visible = True
            Boton_Tablero.Location = New Point(20 + PosicionX, 25 + posicionY)
            Boton_Tablero.Height = 15
            Boton_Tablero.Width = 15
            Boton_Tablero.Tag = Boton_Tablero.Tag + tags
            tags = tags + 1
            Boton_Tablero.Enabled = False
            GBMapa.Controls.Add(Boton_Tablero)
            PosicionX = PosicionX + 15
            CambioLinea = CambioLinea + 1
            'ControlLine
            If CambioLinea = 14 Then
                posicionY = posicionY + 15
                PosicionX = 0
                CambioLinea = 0
            End If
        Next



    End Sub
#End Region 'Lo hace perfecto
#Region "BTN_GENERATE_MAP"
    Private Sub btnGenerarMapa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerarMapa.Click
        'Declaration of variables
        Dim PosicionInicial As Integer = 0
        Dim Numero As New Random
        'Initialization map
        Tablero.Clear()
        Tablero = MiTablero.GenerarMapaAleatorio
        For a = 0 To GBMapa.Controls.Count - 1

            GBMapa.Controls(a).BackColor = Color.Transparent
        Next
        'Drawing board 
        For x = 0 To 195
            If Tablero(x) = 1 Then
                GBMapa.Controls(x).BackColor = Color.Black
            End If
            If Tablero(x) = 2 Then
                GBMapa.Controls(x).BackColor = Color.Red
            End If
        Next
        'Control Exception
        GBMapa.Controls(195).BackColor = Color.Black
        GBMapa.Controls(0).BackColor = Color.Black
        PosicionInicial = Numero.Next(0, 195)
        'Creation PacManColorX
        If GBMapa.Controls(PosicionInicial).BackColor <> Color.Red Or GBMapa.Controls(PosicionInicial).BackColor <> Color.Black Then
            GBMapa.Controls(PosicionInicial).BackColor = Color.Orange
        End If
        'Creation Points
        For i = 0 To GBMapa.Controls.Count - 1
            If GBMapa.Controls(i).BackColor = Color.Transparent Then
                GBMapa.Controls(i).BackColor = Color.Thistle
            End If
        Next

    End Sub
#End Region 'Lo hace perfecto
#Region "BTN_START"
    Private Sub btnComenzar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComenzar.Click
        Reloj.Enabled = True
        Cronometro.Enabled = True
        btnGenerarMapa.Enabled = False
        btnComenzar.Enabled = False
    End Sub
#End Region 'Lo hace correcto
#Region "TIME"

    Private Sub Reloj_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reloj.Tick
        'Declaration of variables
        Dim Numero As New Random
        Dim Sentido As Integer = 0
        'Movement of Ghosts (Switch Case Random)
        'For each test a random position Ghost
        'For i = 0 To 194 'Start FOR
        Dim i As Integer
        Do
            i = Numero.Next(195)
        Loop Until GBMapa.Controls(i).BackColor = Color.Red
        Sentido = Numero.Next(0, 4) 'Elijo Sentido en el que se movera el fantasma
        Select Case Sentido
            '0=Right
            '1=Left
            '2=Below
            '3=Up
            Case 0
                If GBMapa.Controls(i).BackColor = Color.Red AndAlso Not GBMapa.Controls(i + 1).BackColor = Color.Black AndAlso Not GBMapa.Controls(i + 1).BackColor = Color.Red Then
                    If GBMapa.Controls(i + 1).BackColor = Color.Orange Then
                        Reloj.Enabled = False
                        MessageBox.Show("Has perdido!")

                        Exit Sub
                    End If
                    If GBMapa.Controls(i + 1).BackColor = Color.Transparent Then
                        lblPuntos.Text = lblPuntos.Text - 2
                    End If

                    GBMapa.Controls(i).BackColor = Color.Thistle
                    GBMapa.Controls(i + 1).BackColor = Color.Red

                End If
            Case 1

                If GBMapa.Controls(i).BackColor = Color.Red AndAlso Not GBMapa.Controls(i - 1).BackColor = Color.Black AndAlso Not GBMapa.Controls(i - 1).BackColor = Color.Red Then

                    If GBMapa.Controls(i - 1).BackColor = Color.Orange Then
                        Reloj.Enabled = False
                        MessageBox.Show("HBHbg")

                        Exit Sub
                    End If
                    If GBMapa.Controls(i - 1).BackColor = Color.Transparent Then
                        lblPuntos.Text = lblPuntos.Text - 2
                    End If
                    GBMapa.Controls(i).BackColor = Color.Thistle
                    GBMapa.Controls(i - 1).BackColor = Color.Red
                End If
            Case 2

                'Control lastLine
                If i < 182 Then
                    If GBMapa.Controls(i).BackColor = Color.Red AndAlso Not GBMapa.Controls(i + 14).BackColor = Color.Black AndAlso Not GBMapa.Controls(i + 14).BackColor = Color.Red Then
                        If GBMapa.Controls(i + 14).BackColor = Color.Orange Then
                            Reloj.Enabled = False
                            MessageBox.Show("HBHbg")

                        End If
                        If GBMapa.Controls(i + 14).BackColor = Color.Transparent Then
                            lblPuntos.Text = lblPuntos.Text - 2
                        End If
                        GBMapa.Controls(i).BackColor = Color.Thistle
                        GBMapa.Controls(i + 14).BackColor = Color.Red

                    End If
                End If
            Case 3
                If i > 14 Then
                    'Control firstLine
                    If GBMapa.Controls(i).BackColor = Color.Red AndAlso Not GBMapa.Controls(i - 14).BackColor = Color.Black AndAlso Not GBMapa.Controls(i - 14).BackColor = Color.Red Then
                        If GBMapa.Controls(i - 14).BackColor = Color.Orange Then
                            Reloj.Enabled = False
                            MessageBox.Show("HBHbg")

                            Exit Sub
                        End If
                        If GBMapa.Controls(i - 14).BackColor = Color.Transparent Then
                            lblPuntos.Text = lblPuntos.Text - 2
                        End If
                        GBMapa.Controls(i).BackColor = Color.Thistle
                        GBMapa.Controls(i - 14).BackColor = Color.Red

                    End If
                End If
        End Select

        'Next
    End Sub
#End Region 'Tengo que mejorar el movimiento
    Private Sub Cronometro_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cronometro.Tick
        lblTiempo.Text = lblTiempo.Text - 1
        If lblTiempo.Text = 0 Then
            MessageBox.Show("Se acabo el tiempo! DERROTA")
            Cronometro.Enabled = False
        End If
    End Sub
End Class
